// pages/add_donationlist/index.js
var router = require('../../utils/router/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    donationtext:'点击选择物资类别',
    donationlist:['测试物资1','测试物资2','测试物资源3'],
    wlsn:'',
    wlc:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    const data = router.extract(options);
    console.log(data.wlsn); 
    console.log(data.donate_id); 
    if(data.wlsn != null & data.wlc != null){
      this.setData({
        wlsn:data.wlsn,
        wlc:data.wlc,
      })
    }
    if(data.donate_id != null & 
      data.donate_name != null &
      data.hospital_id != null &
      data.hospital_name != null 
      ){
        this.setData({
          donate_id:data.donate_id,
          donate_name:data.donate_name,
          hospital_id: data.hospital_id,
          hospital_name: data.hospital_name
        })
    }

    const s = new Set();
    that.setData({
      donations:s
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 查看历史清单
   */
  bind_all_show_donationlist: function(){
    router.push({ 
      name: 'all_show_donationlist',
      data: {
        ads: ''
        } 
      })
  },

  /**
   * 跳转新增物资源
   */
  add_new_donation: function(){
    var that = this
    var s= that.data.donations
    if(that.data.goods_name == null || 
      that.data.standard == null ||
      that.data.amount == null ||
      that.data.supplier == null){
        return
    }
    s.add({
      "goods_name": that.data.goods_name,
      "standard": that.data.standard,
      "amount": that.data.amount,
      "supplier": that.data.supplier
    })

    console.log(s)
    
    that.setData({
      donations:s
    })
  },
  /**
   * 生成分享订单
   */
  show_donation_list: function(){
    router.push({ 
      name: 'show_donationlist' 
      })
  },

  add_logistics_info: function(){
    var that =  this
    router.push({ 
      name: 'add_logistics_info',
      data: {
        donate_id: that.data.donate_id,
        donate_name:that.data.donate_name,
        hospital_id: that.data.hospital_id,
        hospital_name: that.data.hospital_name
       } 
      })
  },

  /**
   * 物资类别选择
   */
  binddonationChange: function(e){
    var that = this
    this.setData({
      index: e.detail.value,
      donationtext:'',
      goods_name:that.data.donationlist[e.detail.value]
    })
  },

  /**
   * 质量标准
   */
  bindstandInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      standard: e.detail.value
    })
  },

  bindnumsInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      amount: e.detail.value
    })
  },
  bindcInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      supplier: e.detail.value
    })
  },

  /**
   * 新增物资按钮
   */
  save_add_donation: function(){
    var that = this
    if(that.data.donations.size < 1){
      return
    }
    wx.cloud.callFunction({
      // 云函数名称
      name: 'donate',
      // 传给云函数的参数
      data:{
        "action": "add_donated_materials",
        "donate_id": that.data.donate_id,
        "donate_name":  that.data.donate_name,
        "donate_hospital_id": that.data.donate_hospital_id,
        "donate_hospital_name": that.data.donate_hospital_name,
        "materials":Array.from(that.data.donations),
        "logistics": {
          "logistics_order": that.data.wlsn,
          "logistics_company": that.data.wlc
        },
        "content": "紧急捐赠"
      }
      
    })
    .then(res => {
      console.log(res.result) 
      if(res.result.errMsg  == 'collection.add:ok'){
        console.log("捐赠单生成成功!")
      }
    })
    .catch(console.error);
  }


})